import { ref } from 'vue'
import { uploadFile } from '@/api/upload' // 你的上传API
import { ElMessage } from 'element-plus'

// 上传单个文件
export const uploadSingleFile = async (file: File) => {
  try {
    const formData = new FormData()
    formData.append('file', file)

    const response = await uploadFile(formData)

    if (response.code === 200) {
      return response.data // 返回服务器上的文件URL
    } else {
      ElMessage.error(response.message || '上传失败')
      return null
    }
  } catch (error) {
    console.error('上传文件失败:', error)
    ElMessage.error('上传文件失败')
    return null
  }
}

// 上传多个文件
const uploadMultipleFiles = async (files: File[]) => {
  const uploadPromises = files.map((file) => uploadSingleFile(file))
  return Promise.all(uploadPromises)
}

// 在组件中使用
const handleUpload = async (file: File) => {
  const uploadedUrl = await uploadSingleFile(file)
  if (uploadedUrl) {
    // 更新UI或状态
  }
}
